package org.jeecg.modules.basic.entry.controller;


import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;

import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.demo.bed.entity.ImportLwgyBedsA;
import org.jeecg.modules.basic.entry.entity.LwgyFjInout;
import org.jeecg.modules.basic.entry.entity.LwgyInoutRecord;
import org.jeecg.modules.basic.entry.entity.LwgyStudentInout;
import org.jeecg.modules.basic.entry.entity.RechargeBean;
import org.jeecg.modules.basic.entry.mapper.LwgyInoutRecordMapper;
import org.jeecg.modules.basic.entry.service.ILwgyElectricControlService;
import org.jeecg.modules.basic.entry.service.ILwgyInoutRecordService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.basic.entry.util.StringUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 出入管理
 * @Author: jeecg-boot
 * @Date:   2020-12-04
 * @Version: V1.0
 */

 @Api(tags="出入管理")
@RestController
@RequestMapping("/entry/lwgyInoutRecord")
@Slf4j
public class LwgyInoutRecordController extends JeecgController<LwgyInoutRecord, ILwgyInoutRecordService> {
	@Autowired
	private ILwgyInoutRecordService lwgyInoutRecordService;

	 @Autowired
	 private LwgyInoutRecordMapper mapper;
	 @Autowired
	 private ILwgyElectricControlService iLwgyElectricControlService;


	/**
	 * 分页列表查询
	 *
	 * @param lwgyInoutRecord
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "出入管理-分页列表查询")
	@ApiOperation(value="出入管理-分页列表查询", notes="出入管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(LwgyInoutRecord lwgyInoutRecord,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LwgyInoutRecord> queryWrapper = QueryGenerator.initQueryWrapper(lwgyInoutRecord, req.getParameterMap());
		Page<LwgyInoutRecord> page = new Page<LwgyInoutRecord>(pageNo, pageSize);
		IPage<LwgyInoutRecord> pageList = lwgyInoutRecordService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 分页列表查询学生在寝情况
	  *
	  * @param lwgyInoutRecord
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "学生在寝管理-分页列表查询")
	 @ApiOperation(value="学生在寝管理-分页列表查询", notes="学生在寝管理-分页列表查询")
	 @GetMapping(value = "/stuList")
	 public Result<?> queryStuPageList(LwgyStudentInout lwgyInoutRecord,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 QueryWrapper<LwgyStudentInout> queryWrapper = QueryGenerator.initQueryWrapper(lwgyInoutRecord, req.getParameterMap());
		 Page<LwgyStudentInout> page = new Page<LwgyStudentInout>(pageNo, pageSize);

		 //if ( oConvertUtils.isNotEmpty(lwgyInoutRecord.getStudentNumber())){
			// queryWrapper.eq("student_number", lwgyInoutRecord.getStudentNumber());
		 //}
		 //if ( oConvertUtils.isNotEmpty(lwgyInoutRecord.getStudentName())){
			// queryWrapper.eq("student_name", lwgyInoutRecord.getStudentName());
		 //}
		 //IPage<LwgyInoutRecord> pageList = lwgyInoutRecordService.page(page, queryWrapper);
		 IPage<LwgyStudentInout> pageList = lwgyInoutRecordService.stuPageList(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	/**
	 *   添加
	 *
	 * @param lwgyInoutRecord
	 * @return
	 */
	@AutoLog(value = "出入管理-添加")
	@ApiOperation(value="出入管理-添加", notes="出入管理-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody LwgyInoutRecord lwgyInoutRecord) {
		lwgyInoutRecord.setMode("0");//记录模式为人工录入
		Date date = new Date();// 获取当前时间
		lwgyInoutRecord.setRecordDate(date);//当前时间
		lwgyInoutRecordService.save(lwgyInoutRecord);
		String stuid = lwgyInoutRecord.getUserId();//学生id
		String direction = lwgyInoutRecord.getDirection();//1进2出0无
		List<Map<String, String>> stuFj = lwgyInoutRecordService.getStudentFjById(stuid);
		if(stuFj.size()>0) {
			Boolean b = lwgyInoutRecordService.stuBindPower(stuid, direction,stuFj);
			if (b) {
				return Result.OK("添加成功！");
			} else {
				return Result.error("没有住宿信息或房间照明操作失败，请手动关电！");
			}
		}else{
			return Result.OK("添加成功！");
		}
		//return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param lwgyInoutRecord
	 * @return
	 */
	@AutoLog(value = "出入管理-编辑")
	@ApiOperation(value="出入管理-编辑", notes="出入管理-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody LwgyInoutRecord lwgyInoutRecord) {
		lwgyInoutRecordService.updateById(lwgyInoutRecord);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "出入管理-通过id删除")
	@ApiOperation(value="出入管理-通过id删除", notes="出入管理-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		lwgyInoutRecordService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "出入管理-批量删除")
	@ApiOperation(value="出入管理-批量删除", notes="出入管理-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.lwgyInoutRecordService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "出入管理-通过id查询")
	@ApiOperation(value="出入管理-通过id查询", notes="出入管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		LwgyInoutRecord lwgyInoutRecord = lwgyInoutRecordService.getById(id);
		if(lwgyInoutRecord==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(lwgyInoutRecord);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param lwgyInoutRecord
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LwgyInoutRecord lwgyInoutRecord) {
        return super.exportXls(request, lwgyInoutRecord, LwgyInoutRecord.class, "出入管理");
    }

	 /**
	  * 导出excel
	  *
	  * @param request
	  * @param lwgyStudentInout
	  */
	 @RequestMapping(value = "/exportStudentXls")
	 public ModelAndView exportStudentXls(HttpServletRequest request, LwgyStudentInout lwgyStudentInout) {
		 List<LwgyStudentInout> pageList = lwgyInoutRecordService.outStuPageList();
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 mv.addObject(NormalExcelConstants.FILE_NAME, "学生在寝情况");
		 mv.addObject(NormalExcelConstants.CLASS, LwgyStudentInout.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("学生在寝情况", ""));
		 mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
		 return mv;
	 }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LwgyInoutRecord.class);
    }
	 public List<LwgyInoutRecord> getList() {
		 QueryWrapper<LwgyInoutRecord> wrapper = new QueryWrapper<>();
		 List<LwgyInoutRecord> list = mapper.selectList(wrapper);
		 return list;
	 }


	 /**
	  * 分页列表查询
	  *
	  * @param lwgyFjInout
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "出入管理-房间情况汇总")
	 @ApiOperation(value="出入管理-房间情况汇总", notes="出入管理-房间情况汇总")
	 @GetMapping(value = "/totalList")
	 public Result<?> totalList(LwgyFjInout lwgyFjInout,
								@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								HttpServletRequest req) {
		 //QueryWrapper<LwgyFjInout> queryWrapper = QueryGenerator.initQueryWrapper(lwgyFjInout, req.getParameterMap());
		 QueryWrapper<LwgyFjInout> queryWrapper = new QueryWrapper<>();
		 Page<LwgyFjInout> page = new Page<LwgyFjInout>(pageNo, pageSize);
		 queryWrapper.eq("campus_id",lwgyFjInout.getId()).or().eq("building_id",lwgyFjInout.getId()).or().eq("unit_id",lwgyFjInout.getId()).or().eq("layer_id",lwgyFjInout.getId());
		 queryWrapper.groupBy("room_id");
		 queryWrapper.orderByAsc("lbb.id ");

		 IPage<LwgyFjInout> pageList = lwgyInoutRecordService.totalPage(page, queryWrapper);
		 return Result.OK(pageList);
	 }




	 /**
	  * 手动开电
	  *
	  * @param lwgyFjInout
	  *
	  */
	 @AutoLog(value = "出入管理-手动开电")
	 @ApiOperation(value="出入管理-手动开电", notes="出入管理-手动开电")
	 @PostMapping(value = "/toOpenElectricity")
	 public Result<?> toOpenElectricity(@RequestBody LwgyFjInout lwgyFjInout) {
	 	try{
			String xq=lwgyFjInout.getCampusName();
			String lh=lwgyFjInout.getBuildingName();
			String fjh=lwgyFjInout.getRoomName();
			String fj=fjToCode(lh,fjh);
		 	iLwgyElectricControlService.useSBKGData(xq,fj,"1","1");
			return Result.OK();
		}catch(Exception e){
	 		return Result.error("操作失败");
		}

	 }


	 /**
	  * 手动断电
	  *
	  * @param lwgyFjInout
	  *
	  */
	 @AutoLog(value = "出入管理-手动断电")
	 @ApiOperation(value="出入管理-手动断电", notes="出入管理-手动断电")
	 @PostMapping(value = "/toCloseElectricity")
	 public Result<?> toCloseElectricity(@RequestBody LwgyFjInout lwgyFjInout) {
		 try{
			 String xq=lwgyFjInout.getCampusName();
			 String lh=lwgyFjInout.getBuildingName();
			 String fjh=lwgyFjInout.getRoomName();
			 String fj=fjToCode(lh,fjh);

			 iLwgyElectricControlService.useSBKGData(xq,fj,"2","1");
			 return Result.OK();
		 }catch(Exception e){
			 return Result.error("操作失败");
		 }

	 }
	 /**
	  * 查看电控详情
	  *
	  * @param lwgyFjInout
	  *
	  */
	 @AutoLog(value = "出入管理-电控详情")
	 @ApiOperation(value="出入管理-电控详情", notes="出入管理-电控详情")
	 @PostMapping(value = "/toShowElectricity")
	 public Result<?> toShowElectricity(@RequestBody LwgyFjInout lwgyFjInout) {
	 	System.out.println("=====================");
		 try{
			/* String xq=lwgyFjInout.getCampusName();
			 String lh=lwgyFjInout.getBuildingName();
			 String fjh=lwgyFjInout.getRoomName();
			 String fj=fjToCode(lh,fjh);*/
			 String xq = "主校区";
			 String fj="101001";
			 RechargeBean rechargeBean = iLwgyElectricControlService.showDlZTArray(xq,fj);
			 return Result.OK(rechargeBean);
		 }catch(Exception e){
			 return Result.error("操作失败");
		 }

	 }

	 public String fjToCode(String lh,String fjh){
		 String fj="";
		 String tempLh = StringUtil.extractNumber(lh) + "";
		 String tempFjh = fjh;
		 String tempCh = "";// 101101 101001
		 if (tempFjh.length() > 3) {
			 tempCh = tempFjh.substring(0, 2);
			 tempFjh = StringUtil.addZeroForNum(
					 Integer.parseInt(tempFjh.substring(2)), 3);
		 } else {
			 tempCh = "0" + tempFjh.substring(0, 1);
			 tempFjh = StringUtil.addZeroForNum(
					 Integer.parseInt(tempFjh.substring(1)), 3);
		 }
		 return fj = tempLh.concat(tempCh).concat(tempFjh); // 0101001
	 }
	 /**
	  * 模拟开关电
	  *
	  * @param lwgyFjInout
	  *
	  */
	 @AutoLog(value = "出入管理-模拟开电")
	 @ApiOperation(value="出入管理-模拟开电", notes="出入管理-模拟开关电")
	 @PostMapping(value = "/toComeStu")
	 public Result<?> toComeStu(@RequestBody LwgyFjInout lwgyFjInout) {

		 try{
			 String xq=lwgyFjInout.getCampusName();
			 String lh=lwgyFjInout.getBuildingName();
			 String fjh=lwgyFjInout.getRoomName();
			 String fj=fjToCode(lh,fjh);

			 iLwgyElectricControlService.useSBKGData("主校区","101001","1","1");
			 return Result.OK();
		 }catch(Exception e){
			 return Result.error("操作失败");
		 }

	 }

	 /**
	  * 模拟开关电
	  *
	  * @param lwgyFjInout
	  *
	  */
	 @AutoLog(value = "出入管理-模拟关电")
	 @ApiOperation(value="出入管理-模拟关电", notes="出入管理-模拟开关电")
	 @PostMapping(value = "/toGoStu")
	 public Result<?> toGoStu(@RequestBody LwgyFjInout lwgyFjInout) {



		 try{
			 String xq=lwgyFjInout.getCampusName();
			 String lh=lwgyFjInout.getBuildingName();
			 String fjh=lwgyFjInout.getRoomName();
			 String fj=fjToCode(lh,fjh);


			 iLwgyElectricControlService.useSBKGData("主校区","101001","2","1");
			 return Result.OK();
		 }catch(Exception e){
			 return Result.error("操作失败");
		 }

	 }



	 /**
	  * 按照楼宇统计在寝室人数
	  *
	  * @param
	  *
	  */
	 @AutoLog(value = "出入管理-按照楼宇统计在寝室人数")
	 @ApiOperation(value="出入管理-按照楼宇统计在寝室人数", notes="出入管理-按照楼宇统计在寝室人数")
	 @PostMapping(value = "/StaOfDormitoryPopulationByBuilding")
	 public List<Map<String, String>> StaOfDormitoryPopulationByBuilding(String id){
	 	return lwgyInoutRecordService.StaOfDormitoryPopulationByBuilding(id);
	 }

	 /**
	  * 按照学院统计在寝室人数
	  *
	  * @param id
	  *
	  */
	 @AutoLog(value = "出入管理-按照学院统计在寝室人数")
	 @ApiOperation(value="出入管理-按照学院统计在寝室人数", notes="出入管理-按照按照学院统计在寝室人数楼宇统计在寝室人数")
	 @PostMapping(value = "/StaOfDormitoryPopulationByCollege")
	 public
	 List<Map<String, String>> StaOfDormitoryPopulationByCollege(String id){
		 return lwgyInoutRecordService.StaOfDormitoryPopulationByCollege(id);
	 }


}
